package com.bt7274.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bt7274.mapper.PromotionPolicyMapper;
import com.bt7274.pojo.PromotionPolicy;
import com.bt7274.service.PromotionPolicyService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Data 2025/5/28 12:10
 * @Author hp
 * @Version 1.0
 * @Description PromotionPolicyServiceImpl
 */
@Service
public class PromotionPolicyServiceImpl implements PromotionPolicyService {
    @Resource
    private PromotionPolicyMapper promotionPolicyMapper;



    @Override
    public PromotionPolicy getPromotionPolicyBySlug(String slug) {
        return promotionPolicyMapper.selectOne(
                new QueryWrapper<PromotionPolicy>().eq("slug", slug)
        );
    }

    @Override
    public PromotionPolicy getDefaultPromotionPolicy() {
        return promotionPolicyMapper.selectOne(
                new QueryWrapper<PromotionPolicy>().eq("slug", "normal")
        );
    }

    @Override
    public List<PromotionPolicy> queryAll() {
        return promotionPolicyMapper.selectList(
                new QueryWrapper<PromotionPolicy>()
        );
    }

    @Override
    public PromotionPolicy getPromotionPolicyById(Long id) {
        return promotionPolicyMapper.selectById(id);
    }

    @Override
    @Transactional
    public PromotionPolicy createPromotionPolicy(PromotionPolicy policy) {
        promotionPolicyMapper.insert(policy);
        return policy;
    }

    @Override
    @Transactional
    public PromotionPolicy updatePromotionPolicy(PromotionPolicy policy) {
        promotionPolicyMapper.updateById(policy);
        return policy;
    }

    @Override
    @Transactional
    public boolean deletePromotionPolicy(Long id) {
        return promotionPolicyMapper.deleteById(id) > 0;
    }



//    @Override
//    public List<PromotionPolicy> getApplicablePolicies(String userId, String targetType) {
//        QueryWrapper<PromotionPolicy> wrapper = new QueryWrapper<PromotionPolicy>()
//                .eq("enabled", true)
//                .eq("target_type", targetType)
//                .and(w -> w
//                        .eq("target_user", "ALL")
//                        .or()
//                        .eq("target_user", isVip ? "VIP" : "NORMAL")
//                )
//                .le("start_time", LocalDateTime.now())
//                .ge("end_time", LocalDateTime.now());
//
//        return promotionPolicyMapper.selectList(wrapper);
//    }
}
